home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / chat / reflect.000 / reflect / 3.0b3 / socket.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-06  |  22.7 KB  |  791 lines

  1. /*
  2.  
  3. Copyright 1993, 1994, Cornell University
  4.  
  5. Cornell hereby grants permission to use, copy, modify, and distribute this program for any purpose 
  6. and without fee, provided that these copyright and permission notices appear on all copies and 
  7. supporting documentation, the name of Cornell not be used in advertising or publicity pertaining 
  8. to distribution of the program without specific prior permission, notice be given in supporting 
  9. documentation that copying and distribution is by permission of Cornell.  CORNELL MAKES NO 
  10. REPRESENTATIONS OR WARRANTEES, EXPRESS OR IMPLIED.  By way of example, but not limitation, 
  11. CORNELL MAKES NO REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR 
  12. PURPOSE OR THAT THE USE OF THIS SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY PATENTS, COPYRIGHTS, 
  13. TRADEMARKS, OR OTHER RIGHTS.  Cornell shall not be held liable for any liability with respect to 
  14. any claim by the user or any other party arising from use of the program.
  15.  
  16. This material is partially based on work sponsored by the National Science Foundation under Cooperative 
  17. Agreement No. NCR-9318337.  The government has certain rights in this material.
  18.  
  19. */
  20.  
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <signal.h>
  25. #include <sys/param.h>
  26. #include <sys/file.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29.  
  30. #ifndef LINUX
  31. #include <sys/socketvar.h>
  32. #endif
  33.  
  34. #include <netinet/in.h>
  35.  
  36. #include <unistd.h>
  37. #include <fcntl.h>
  38.  
  39. #include <sys/time.h>
  40. #include <errno.h>
  41. #include "reflect.h"
  42. #include "globals.h"
  43.  
  44. char                        hostname[MAXHOSTNAMELEN];
  45.  
  46.  
  47. void init_socket()
  48. {
  49.     struct sockaddr_in  saddr;
  50. #ifdef MULTI
  51.     struct ip_mreq mreq;
  52. #endif
  53.     unsigned char ttl;
  54.     unsigned char loop;
  55.  
  56.     
  57.  
  58.     if (nv_ucast_port != 0){
  59.       
  60.       if ((nv_ucast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  61.         my_perror("socket: nv_ucast socket");
  62.       
  63.       saddr.sin_family = AF_INET;
  64.       saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  65.       saddr.sin_port = htons(nv_ucast_port);
  66.       
  67.       if (bind(nv_ucast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  68.         my_perror("bind error port, nv_ucast_sock");
  69.       
  70.     }
  71.  
  72.     if (maven_port != 0)
  73.     {
  74.  
  75.        if ((maven_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  76.            my_perror("socket: maven socket");
  77.  
  78.        saddr.sin_family = AF_INET;
  79.        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  80.        saddr.sin_port = htons(maven_port);
  81.  
  82.        if (bind(maven_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  83.            my_perror("bind error port, maven_sock");
  84.  
  85.        if ((maven_cntl_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  86.            my_perror("socket: maven control socket");
  87.  
  88.        saddr.sin_family = AF_INET;
  89.        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  90.        saddr.sin_port = htons(maven_port+1);
  91.  
  92.        if (bind(maven_cntl_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  93.            my_perror("bind error port, maven_cntl_sock");
  94.  
  95.        if ((maven_cntl_sock_out = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  96.            my_perror("socket: maven control socket out");
  97.  
  98.        saddr.sin_family = AF_INET;
  99.        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  100.        saddr.sin_port = htons(0);
  101.  
  102.        if (bind(maven_cntl_sock_out, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  103.            my_perror("bind error port, maven_cntl_sock_out");
  104.     }
  105.  
  106.     if ((vid_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  107.         my_perror("socket: video socket");
  108.  
  109.     saddr.sin_family = AF_INET;
  110.     saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  111.     saddr.sin_port = htons(VID_PORT);
  112.  
  113.     if (bind(vid_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  114.         my_perror("bind error port, vid_sock");
  115.  
  116.     if ((cntrl_sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  117.         my_perror("socket: control socket");
  118.  
  119.     saddr.sin_family = AF_INET;
  120.     saddr.sin_addr.s_addr = INADDR_ANY;
  121.     saddr.sin_port = htons(CONTROL_PORT);
  122.  
  123.     if (bind(cntrl_sock, &saddr, sizeof(struct sockaddr_in)) < 0)
  124.         my_perror("bind error port, cntrl_sock");
  125.  
  126. #ifdef MULTI
  127.     if (rfout_mcast.sin_addr.s_addr)
  128.     {
  129.        if ((rfout_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  130.            my_perror("socket: rfout multicast socket");
  131.  
  132.        saddr.sin_family = AF_INET;
  133.        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  134.        saddr.sin_port = htons(RF_PORT);
  135.  
  136.        if (bind(rfout_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  137.            my_perror("bind error port");
  138.  
  139.        ttl = rf_ttl;
  140.        setsockopt(rfout_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  141.     }
  142.  
  143.     if (rfin_mcast.sin_addr.s_addr)
  144.     {
  145.        if ((rfin_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  146.            my_perror("socket: rfin multicast socket");
  147.  
  148.        saddr.sin_family = AF_INET;
  149.        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  150.        saddr.sin_port = htons(RF_PORT);
  151.  
  152.        if (bind(rfin_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  153.            my_perror("bind error port");
  154.  
  155.        mreq.imr_multiaddr.s_addr = rfin_mcast.sin_addr.s_addr;
  156.        mreq.imr_interface.s_addr = INADDR_ANY;
  157.  
  158.        setsockopt(rfin_mcast_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
  159.     }
  160.  
  161.     if (inout_mcast.sin_addr.s_addr)
  162.     {
  163.        if ((inout_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  164.            my_perror("socket: inout multicast socket");
  165.  
  166.        saddr.sin_family = AF_INET;
  167.        saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  168.        saddr.sin_port = htons(RF_PORT);
  169.  
  170.        if (bind(inout_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  171.            my_perror("bind error port");
  172.  
  173.        ttl = inout_ttl;
  174.        setsockopt(inout_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  175.  
  176.        mreq.imr_multiaddr.s_addr = inout_mcast.sin_addr.s_addr;
  177.        mreq.imr_interface.s_addr = INADDR_ANY;
  178.  
  179.        setsockopt(inout_mcast_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
  180.  
  181.        loop = 0;
  182.        setsockopt(inout_mcast_sock,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop));
  183.     }
  184.  
  185.  
  186.     /* if the config file has given us a vat-mc-port and no vat-mc-in/out multicast
  187.        address, then it has been configured incorrectly, and we should exit. */
  188.  
  189.     if ((vat_port) && !(vat_in_mcast.sin_addr.s_addr) && !(vat_out_mcast.sin_addr.s_addr)){
  190.       printf("No vat multicast in or out address given w/ vat-mc-port.\n");
  191.       exit(-1);
  192.     }
  193.  
  194.  
  195.     /* Bind the appropriate ports if we have read vat-mc-in/out from the config file.  */
  196.     if ((vat_in_mcast.sin_addr.s_addr) || (vat_out_mcast.sin_addr.s_addr)){
  197.  
  198.       if (vat_port == 0){
  199.         printf("No vat multicast port provided in config file.\n");
  200.         exit(-1);
  201.       }
  202.  
  203.       /* vat mcast receive only */
  204.       if ((vat_in_mcast.sin_addr.s_addr) && !(vat_out_mcast.sin_addr.s_addr)){
  205.      
  206.         if ((vat_in_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  207.           my_perror("socket: vat_in_mcast socket");
  208.       
  209.         saddr.sin_family = AF_INET;
  210.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  211.         saddr.sin_port = htons(vat_port);
  212.  
  213.         if (bind(vat_in_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  214.           my_perror("bind error port");
  215.  
  216.         mreq.imr_multiaddr.s_addr = vat_in_mcast.sin_addr.s_addr;
  217.         mreq.imr_interface.s_addr = INADDR_ANY;
  218.  
  219.         setsockopt(vat_in_mcast_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
  220.         if (debug)
  221.           printf("vat_in_mcast_sock bound\n");
  222.  
  223.       }
  224.       
  225.  
  226.       /* vat mcast send only */
  227.       else if (!(vat_in_mcast.sin_addr.s_addr) && (vat_out_mcast.sin_addr.s_addr)){
  228.  
  229.         if ((vat_out_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  230.            my_perror("socket: vat_out_mcast socket");
  231.  
  232.         saddr.sin_family = AF_INET;
  233.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  234.         saddr.sin_port = htons(vat_port);
  235.         
  236.         if (bind(vat_out_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  237.           my_perror("bind error port");
  238.  
  239.         ttl = vat_ttl;
  240.         setsockopt(vat_out_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  241.  
  242.  
  243.         /*
  244.         loop = 0;
  245.         setsockopt(vat_out_mcast_sock,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop));
  246.         */
  247.  
  248.         if (debug)
  249.           printf("vat_out_mcast_sock bound\n");
  250.       
  251.       }
  252.  
  253.       /* vat mcast send & receive */
  254.       else if (((vat_in_mcast.sin_addr.s_addr) && (vat_out_mcast.sin_addr.s_addr)) && 
  255.           (vat_in_mcast.sin_addr.s_addr) == (vat_out_mcast.sin_addr.s_addr)){
  256.  
  257.         vat_inout_mcast.sin_addr.s_addr = vat_out_mcast.sin_addr.s_addr;
  258.  
  259.         if ((vat_inout_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  260.           my_perror("socket: vat_inout_mcast socket");
  261.         
  262.         saddr.sin_family = AF_INET;
  263.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  264.         saddr.sin_port = htons(vat_port);
  265.  
  266.         if (bind(vat_inout_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  267.           my_perror("bind error port");
  268.  
  269.         ttl = vat_ttl;
  270.         setsockopt(vat_inout_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  271.  
  272.         mreq.imr_multiaddr.s_addr = vat_inout_mcast.sin_addr.s_addr;
  273.         mreq.imr_interface.s_addr = INADDR_ANY;
  274.  
  275.         setsockopt(vat_inout_mcast_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
  276.  
  277.         loop = 0;
  278.         setsockopt(vat_inout_mcast_sock,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop));
  279.  
  280.         if (debug)
  281.           printf("vat_inout_mcast_sock bound\n");
  282.       }
  283.     
  284.       else{
  285.         printf("Can't have different vat-mc-in and vat-mc-out multicast addresses!");
  286.         exit(-1);
  287.       }
  288.  
  289.       /* bind the vat control socket */
  290.       if ((vat_cntl_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  291.         my_perror("socket: vat_cntl_mcast socket");
  292.  
  293.       saddr.sin_family = AF_INET;
  294.       saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  295.       saddr.sin_port = htons(vat_port+1);
  296.  
  297.       if (bind(vat_cntl_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  298.         my_perror("bind error port, vat_cntl_mcast_sock");
  299.  
  300.       ttl = vat_ttl;
  301.       setsockopt(vat_cntl_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  302.  
  303.       loop = 0;
  304.       setsockopt(vat_cntl_mcast_sock,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop));
  305.  
  306.       if (debug)
  307.         printf("vat_cntl_mcast_sock bound\n");
  308.     }
  309.  
  310.  
  311.         
  312.       
  313.   /* if the config file has given us a nv-mc-port and no nv-mc-in/out multicast
  314.        address, then it has been configured incorrectly, and we should exit. */
  315.  
  316.     if ((nv_mcast_port) && !(nv_in_mcast.sin_addr.s_addr) && !(nv_out_mcast.sin_addr.s_addr)){
  317.       printf("No nv multicast in or out address given w/ nv-mc-port.\n");
  318.       exit(-1);
  319.     }
  320.  
  321.  
  322.     /* Bind the appropriate ports if we have read nv-mc-in/out from the config file.  */
  323.     if ((nv_in_mcast.sin_addr.s_addr) || (nv_out_mcast.sin_addr.s_addr)){
  324.  
  325.       if (nv_mcast_port == 0){
  326.         printf("No nv multicast port provided in config file.\n");
  327.         exit(-1);
  328.       }
  329.  
  330.       /* nv mcast receive only */
  331.       if ((nv_in_mcast.sin_addr.s_addr) && !(nv_out_mcast.sin_addr.s_addr)){
  332.      
  333.         if ((nv_in_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
  334.           my_perror("socket: nv_in_mcast socket");
  335.       
  336.         saddr.sin_family = AF_INET;
  337.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  338.         saddr.sin_port = htons(nv_mcast_port);
  339.  
  340.         if (bind(nv_in_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  341.           my_perror("bind error port");
  342.  
  343.         mreq.imr_multiaddr.s_addr = nv_in_mcast.sin_addr.s_addr;
  344.         mreq.imr_interface.s_addr = INADDR_ANY;
  345.  
  346.         setsockopt(nv_in_mcast_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
  347.         if (debug)
  348.           printf("nv_in_mcast_sock bound\n");
  349.  
  350.       }
  351.       
  352.  
  353.       /* nv mcast send only */
  354.       else if (!(nv_in_mcast.sin_addr.s_addr) && (nv_out_mcast.sin_addr.s_addr)){
  355.  
  356.         if ((nv_out_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  357.            my_perror("socket: nv_out_mcast socket");
  358.  
  359.         saddr.sin_family = AF_INET;
  360.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  361.         saddr.sin_port = htons(nv_mcast_port);
  362.         
  363.         if (bind(nv_out_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  364.           my_perror("bind error port");
  365.  
  366.         ttl = nv_ttl;
  367.         setsockopt(nv_out_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  368.  
  369.  
  370.         /*
  371.         loop = 0;
  372.         setsockopt(nv_out_mcast_sock,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop));
  373.         */
  374.  
  375.         if (debug)
  376.           printf("nv_out_mcast_sock bound\n");
  377.       
  378.       }
  379.  
  380.       /* nv mcast send & receive */
  381.       else if (((nv_in_mcast.sin_addr.s_addr) && (nv_out_mcast.sin_addr.s_addr)) && 
  382.           (nv_in_mcast.sin_addr.s_addr) == (nv_out_mcast.sin_addr.s_addr)){
  383.  
  384.         nv_inout_mcast.sin_addr.s_addr = nv_out_mcast.sin_addr.s_addr;
  385.         nv_inout_mcast.sin_family = AF_INET;
  386.         nv_inout_mcast.sin_port = htons(nv_mcast_port);
  387.  
  388.         if ((nv_inout_mcast_sock = socket(AF_INET, SOCK_DGRAM, 0))  < 0)
  389.           my_perror("socket: nv_inout_mcast socket");
  390.         
  391.         saddr.sin_family = AF_INET;
  392.         saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  393.         saddr.sin_port = htons(nv_mcast_port);
  394.  
  395.         if (bind(nv_inout_mcast_sock, (struct sockaddr *) &saddr, sizeof(saddr)) < 0)
  396.           my_perror("bind error port -- nv_inout_mcast_sock");
  397.  
  398.         ttl = nv_ttl;
  399.         setsockopt(nv_inout_mcast_sock,IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
  400.  
  401.         mreq.imr_multiaddr.s_addr = nv_inout_mcast.sin_addr.s_addr;
  402.         mreq.imr_interface.s_addr = INADDR_ANY;
  403.  
  404.         setsockopt(nv_inout_mcast_sock,IPPROTO_IP,IP_ADD_MEMBERSHIP,&mreq,sizeof(mreq));
  405.  
  406.         loop = 0;
  407.         setsockopt(nv_inout_mcast_sock,IPPROTO_IP,IP_MULTICAST_LOOP,&loop,sizeof(loop));
  408.  
  409.         if (debug)
  410.           printf("nv_inout_mcast_sock bound\n");
  411.       }
  412.     
  413.       else{
  414.         printf("Can't have different nv-mc-in and nv-mc-out multicast addresses!");
  415.         exit(-1);
  416.       }
  417.       
  418.     }
  419.  
  420.  
  421.  
  422.  
  423.  
  424. #endif 
  425.  
  426.     if ((fcntl(vid_sock, F_SETFL, FNDELAY)) < 0 )
  427.         my_perror("fcntl error");
  428.  
  429.     if (maven_sock)
  430.     {
  431.        if ((fcntl(maven_sock, F_SETFL, FNDELAY)) < 0 )
  432.            my_perror("fcntl error");
  433.  
  434.        if ((fcntl(maven_cntl_sock, F_SETFL, FNDELAY)) < 0 )
  435.            my_perror("fcntl error");
  436.     }
  437.  
  438.     if ((fcntl(cntrl_sock, F_SETFL, FNDELAY)) < 0) 
  439.         my_perror("fcntl error");
  440.  
  441.     if (gethostname(hostname,MAXHOSTNAMELEN) < 0)
  442.         my_perror("getnostname");
  443.  
  444.     msg_sock = 0;
  445.     setsockopt(cntrl_sock,SOL_SOCKET,SO_REUSEADDR,1,0);
  446.     listen(cntrl_sock,5);
  447. }
  448.  
  449. void init_timer()
  450. {
  451.     struct itimerval    value;
  452.     struct itimerval    ovalue;
  453.     void                sighandler();
  454.  
  455.     value.it_interval.tv_sec = 1;
  456.     value.it_interval.tv_usec = 0;
  457.     value.it_value.tv_sec = 1;
  458.     value.it_value.tv_usec = 0;
  459.  
  460.     setitimer(ITIMER_REAL, &value, &ovalue);
  461.  
  462.     signal(SIGALRM, sighandler);
  463. }
  464.  
  465. void sighandler()
  466. {
  467.     timer_expired++;
  468.     signal(SIGALRM, sighandler);
  469. }
  470.  
  471. int receive(pkt,msglen,caddr)
  472.     char               *pkt;
  473.     int                *msglen;
  474.     struct sockaddr_in *caddr; 
  475. {
  476.     fd_set                      readfds;
  477.     int                         cadrlen,ready;
  478.  
  479.     while (1)
  480.     {
  481.        if (timer_expired)
  482.        {
  483.           timer_expired--;
  484.           do_timer();
  485.        }
  486.  
  487.        FD_ZERO(&readfds);
  488.        FD_SET(vid_sock, &readfds);
  489.  
  490.        if (maven_sock)
  491.        {
  492.           FD_SET(maven_sock,&readfds);
  493.           FD_SET(maven_cntl_sock,&readfds);
  494.        }
  495.        
  496.        if (nv_ucast_sock) 
  497.          FD_SET(nv_ucast_sock,&readfds);
  498.  
  499. #ifdef MULTI
  500.        if (rfin_mcast.sin_addr.s_addr)
  501.          FD_SET(rfin_mcast_sock,&readfds);
  502.        
  503.        if (inout_mcast.sin_addr.s_addr)
  504.          FD_SET(inout_mcast_sock,&readfds);
  505.  
  506.  
  507.        /* if we want to receive, set the appropriate bits in 
  508.           the readfds structure for the in and cntl vat sockets */
  509.  
  510.        if (vat_in_mcast.sin_addr.s_addr){
  511.          FD_SET(vat_in_mcast_sock,&readfds);
  512.          FD_SET(vat_cntl_mcast_sock,&readfds);
  513.  
  514.        }
  515.  
  516.        /* same as above, but if we are sending and receiving */
  517.        if (vat_inout_mcast.sin_addr.s_addr){
  518.          FD_SET(vat_inout_mcast_sock,&readfds);
  519.          FD_SET(vat_cntl_mcast_sock,&readfds);
  520.  
  521.        }
  522.        
  523.        /* if we are just sending, we don't need to read any
  524.           control signals -- do we?  Commented out for now */
  525.  
  526.        /*
  527.        if (vat_out_mcast.sin_addr.s_addr)
  528.          FD_SET(vat_cntl_mcast_sock,&readfds);
  529.        */
  530.        
  531.        /* now do the same for the nv sockets as we just did for the vat ones */
  532.  
  533.        if (nv_in_mcast.sin_addr.s_addr)
  534.          FD_SET(nv_in_mcast_sock,&readfds);
  535.  
  536.        if (nv_inout_mcast.sin_addr.s_addr)
  537.          FD_SET(nv_inout_mcast_sock,&readfds);
  538.        
  539.  
  540. #endif
  541.        if (msg_sock == 0)
  542.           FD_SET(cntrl_sock, &readfds);
  543.  
  544.        if (msg_sock != 0)
  545.           FD_SET(msg_sock, &readfds);
  546.        
  547.        if ((ready = select(FD_SETSIZE, &readfds, NULL, NULL, NULL)) < 0)
  548.        {
  549.           if (errno == EINTR)
  550.              continue;
  551.           else
  552.              my_perror("select error");
  553.        }
  554.  
  555.        if (ready == 0)
  556.            continue;
  557.  
  558.        cadrlen = sizeof(struct sockaddr_in);
  559.  
  560.        if (FD_ISSET(vid_sock, &readfds)) 
  561.        {
  562.           
  563.           if ((*msglen = recvfrom(vid_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  564.           {
  565.          continue;    /* not doing this makes reflector crash easily. */
  566.              if (errno == EINTR)
  567.                 continue;
  568.  
  569.              my_perror("recvfrom error on vid_sock");
  570.           }
  571.  
  572.           return(VIDEO);
  573.        }
  574.  
  575.        if (FD_ISSET(nv_ucast_sock, &readfds)) 
  576.        {
  577.           
  578.          if ((*msglen = recvfrom(nv_ucast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  579.          {
  580.              if (errno == EINTR)
  581.                 continue;
  582.  
  583.              my_perror("recvfrom error on nv_ucast_sock");
  584.           }
  585.           if (debug)
  586.              printf("packet received through nv_ucast_sock\n");
  587.           return(NV_UCAST);
  588.        }
  589.  
  590.  
  591.        if (FD_ISSET(maven_sock, &readfds)) 
  592.        {
  593.           
  594.           if ((*msglen = recvfrom(maven_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  595.           {
  596.              if (errno == EINTR)
  597.                 continue;
  598.  
  599.              my_perror("recvfrom error on maven_sock");
  600.           }
  601.           if (debug)
  602.              printf("packet received through maven_sock\n");
  603.           return(MAVEN);
  604.        }
  605.  
  606.        if (FD_ISSET(maven_cntl_sock, &readfds)) 
  607.        {
  608.           
  609.           if ((*msglen = recvfrom(maven_cntl_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  610.           {
  611.              if (errno == EINTR)
  612.                 continue;
  613.  
  614.              my_perror("recvfrom error on maven_cntl_sock");
  615.           }
  616.           if (debug)
  617.              printf("\npacket received on maven_cntl_sock %s \n", inet_ntoa(caddr->sin_addr));
  618.         
  619.           return(MAVEN_CNTL);
  620.        }
  621.  
  622. #ifdef MULTI
  623.        if ((rfin_mcast_sock) && (FD_ISSET(rfin_mcast_sock, &readfds)))
  624.        {
  625.           
  626.           if ((*msglen = recvfrom(rfin_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  627.           {
  628.              if (errno == EINTR)
  629.                 continue;
  630.  
  631.              my_perror("recvfrom error on rfin_mcast_sock");
  632.           }
  633.  
  634.           return(REF1VIDEO);
  635.        }
  636.  
  637.        if ((inout_mcast_sock) && (FD_ISSET(inout_mcast_sock, &readfds)))
  638.        {
  639.           
  640.           if ((*msglen = recvfrom(inout_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  641.           {
  642.              if (errno == EINTR)
  643.                 continue;
  644.  
  645.              my_perror("recvfrom error on inout_mcast_sock");
  646.           }
  647.  
  648.           return(REF2VIDEO);
  649.        }
  650.  
  651.        if ((vat_in_mcast_sock) && (FD_ISSET(vat_in_mcast_sock, &readfds))){
  652.  
  653.          if  ((*msglen = recvfrom(vat_in_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0) {
  654.            
  655.            if (errno == EINTR)
  656.              continue;
  657.  
  658.            my_perror("recvfrom error on vat_in_mcast_sock");
  659.  
  660.          }
  661.          if (debug)
  662.             printf("packet received on vat_in_mcast_sock\n");
  663.          return(VAT);
  664.  
  665.        }
  666.  
  667.        if ((vat_inout_mcast_sock) && (FD_ISSET(vat_inout_mcast_sock, &readfds))){
  668.  
  669.          if  ((*msglen = recvfrom(vat_inout_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0) {
  670.            
  671.            if (errno == EINTR)
  672.              continue;
  673.  
  674.            my_perror("recvfrom error on vat_inout_mcast_sock");
  675.  
  676.          }
  677.          if (debug)
  678.             printf("packet received on vat_inout_mcast_sock\n");
  679.          return(VAT);
  680.  
  681.        }
  682.  
  683.        if (FD_ISSET(vat_cntl_mcast_sock, &readfds)) 
  684.        {
  685.                   
  686.           if ((*msglen = recvfrom(vat_cntl_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0)
  687.           {
  688.              if (errno == EINTR)
  689.                 continue;
  690.  
  691.              my_perror("recvfrom error on vat_cntl_mcast_sock");
  692.           }
  693.         
  694.           if (debug)
  695.              printf("\npacket received on vat_cntl_sock %s \n", inet_ntoa(caddr->sin_addr));
  696.           return(VAT_CNTL);
  697.        }
  698.  
  699.        
  700.        if ((nv_in_mcast_sock) && (FD_ISSET(nv_in_mcast_sock, &readfds))){
  701.          
  702.          if  ((*msglen = recvfrom(nv_in_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0) {
  703.            
  704.            if (errno == EINTR)
  705.              continue;
  706.  
  707.            my_perror("recvfrom error on nv_in_mcast_sock");
  708.  
  709.          }
  710.          if (debug)
  711.             printf("packet received on nv_in_mcast_sock\n");
  712.          return(NV_MCAST);
  713.  
  714.        }
  715.  
  716.        if ((nv_inout_mcast_sock) && (FD_ISSET(nv_inout_mcast_sock, &readfds))){
  717.  
  718.          if  ((*msglen = recvfrom(nv_inout_mcast_sock,pkt,MAXMSG,0,caddr,&cadrlen)) < 0) {
  719.            
  720.            if (errno == EINTR)
  721.              continue;
  722.  
  723.            my_perror("recvfrom error on nv_inout_mcast_sock");
  724.  
  725.          }
  726.          if (debug)
  727.             printf("packet received on nv_inout_mcast_sock\n");
  728.          return(NV_MCAST);
  729.  
  730.        }
  731.        
  732. #endif
  733.  
  734.        if (FD_ISSET(cntrl_sock, &readfds))
  735.        {
  736.           if (msg_sock != 0)
  737.           {
  738.              dolog("control message with a non-zero msg_sock\n");
  739.              exit(1);
  740.           }
  741.  
  742.           if ((msg_sock = accept(cntrl_sock,caddr,&cadrlen)) < 0)
  743.           {
  744.              if (errno == EINTR)
  745.              {
  746.                 msg_sock = 0;
  747.                 continue;
  748.              }
  749.  
  750.              my_perror("accept");
  751.              exit(1);
  752.           }
  753.  
  754.  
  755.           if ((control_ip == 0) || ((control_ip != -1) && (control_ip != caddr->sin_addr.s_addr)))
  756.           {
  757.              dolog("Control message from %s is disallowed\n",inet_ntoa(caddr->sin_addr));
  758.              close(msg_sock);
  759.              msg_sock = 0;
  760.              continue;
  761.           }
  762.  
  763.  
  764.           dolog("incoming control message new msg_sock is %d\n",msg_sock);
  765.           setsockopt(msg_sock,SOL_SOCKET,SO_REUSEADDR,1,0);
  766.           continue;
  767.         }
  768.  
  769.        if (FD_ISSET(msg_sock, &readfds))
  770.        {
  771.           if ((*msglen = recv(msg_sock,pkt,MAXMSG,0)) < 0)
  772.           {
  773.              if (errno == EINTR)
  774.                 continue;
  775.  
  776.              my_perror("recvfrom error on msg_sock");
  777.              exit (1);
  778.           }
  779.    
  780.           if (*msglen == 0)
  781.           {
  782.              close(msg_sock);
  783.              msg_sock = 0;
  784.              continue;
  785.           }
  786.    
  787.           return (CONTROL);
  788.        }
  789.     }
  790. }
  791.